package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.Order;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import com.jt.service.DubboOrderService;
import com.jt.util.UserThreadLocal;
import com.jt.vo.SysResult;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
/*
    <input    type="text" name="dog.age"     value="15"/>
    <input    type="text" name="hobbies[0]"     value="敲代码"/>
*/
/* class Dog{
    private String name;
    private Integer age;
}
class User{
    private String name;
    private Integer age;
    private Dog dog;
    private List<String> hobbies;
}
public void xxx(User user){

}*/

@Controller
@RequestMapping("order")
public class OrderController {
    @Reference(check=false)
    private DubboCartService dubboCartService;
    @Reference(check = false)
    private DubboOrderService dubboOrderService;

    /**
     * 实现订单去人页面的跳转
     * url: http://www.jt.com/order/create.html
     * 请求参数: userId
     * 返回值结果:订单的确认页面->order-cart.jsp
     * 页面取值: carts
     */
    @RequestMapping("create")
    public String orderCart(Model model){
        Long userId = UserThreadLocal.getUser().getId();
        List<Cart> cartList = dubboCartService.findCartList(userId);
        model.addAttribute("carts",cartList);
        return "order-cart";
    }

    /**
     * 实现入库操作
     * url: http://www.jt.com/order/submit
     * 参数: 整个表单的序列化  Order对象接收
     * 返回值: SysResult(orderId)
     */
    @RequestMapping("/submit")
    @ResponseBody
    public SysResult saveOrder(Order order){
        //动态获取userId
        long userId = UserThreadLocal.getUser().getId();
        order.setUserId(userId);
        String orderId=dubboOrderService.saveOrder(order);
        if (StringUtils.hasLength(orderId))
            return SysResult.success(orderId);
        return SysResult.fail();
    }

    /**
     * 实现订单页的查询
     * url: http://www.jt.com/order/success.html?id=71608194051096
     * 参数: id=xxx
     * 返回值: 跳转到成功页面
     * 页面取值: order对象及封装的两个属性
     */
    @RequestMapping("success")
    public String success(@RequestParam("id") String orderId,Model model){
        Order order = dubboOrderService.findOrderById(orderId);
        model.addAttribute("order",order);
        return "success";
    }
    /**
     * 跳转到我的订单
     * url: http://www.jt.com/order/myOrder.html
     *
     *
     */
    @RequestMapping("myOrder")
    public String showMyOrder(){
        User user = UserThreadLocal.getUser();
        //dubboOrderService.findOrderByUserId(user.getId());
        return "my-orders";
    }
}
